<script>
import axios from 'axios'
import { ElMessageBox, ElMessage } from 'element-plus'

export default {
  name: 'UserManage',
  data() {
    return {
      data: [],
      dialogVisible: false,
      editDialogVisible: false,
      form: {
        username: '',
        userStudentId: '',
        userClass: '',
        userDepartment: '',
        userEmail: ''
      },
      editForm: {
        username: '',
        userStudentId: '',
        userClass: '',
        userDepartment: '',
        userEmail: ''
      }
    }
  },
  methods: {
    async getData() {
      try {
        const res = await axios.get('http://localhost:8080/user/allUser')
        this.data = res.data.data
      } catch (error) {
        console.log(error)
      }
    },

    // 添加用户
    async addUser() {
      try {
        await axios.post('http://localhost:8080/user/addUser', this.form)
        ElMessage.success('添加成功')
        this.dialogVisible = false
        this.getData()
        this.form = {
          username: '',
          userStudentId: '',
          userClass: '',
          userDepartment: '',
          userEmail: ''
        }
      } catch (error) {
        console.error(error)
        ElMessage.error('添加失败')
      }
    },

    // 删除用户
    async deleteUser(row) {
      try {
        await ElMessageBox.confirm(`确定要删除 ${row.username} 吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await axios.post(`http://localhost:8080/user/deleteUser`,
          {
            id:row.id,
          })
        ElMessage.success('删除成功')
        await this.getData()
      } catch (error) {
        if (error !== 'cancel') {
          console.error(error)
          ElMessage.error('删除失败')
        }
      }
    },

    // 打开编辑弹窗
    openEditDialog(row) {
      this.editForm = { ...row }
      this.editDialogVisible = true
    },

    // 保存编辑
    async saveEdit() {
      try {
        await axios.post('http://localhost:8080/user/updateUser', this.editForm)
        ElMessage.success('修改成功')
        this.editDialogVisible = false
        this.getData()
      } catch (error) {
        console.error(error)
        ElMessage.error('修改失败')
      }
    }
  },
  mounted() {
    this.getData()
  }
}
</script>

<template>
  <el-card class="card">
    <h1>用户管理</h1>
    <div style="margin-bottom: 16px; text-align: right;">
      <el-button type="primary" @click="dialogVisible = true">新增用户</el-button>
    </div>

    <el-table :data="data" stripe border>
      <el-table-column prop="username" label="姓名" />
      <el-table-column prop="userStudentId" label="学号" />
      <el-table-column prop="userClass" label="班级" />
      <el-table-column prop="userDepartment" label="部门" />
      <el-table-column prop="userEmail" label="邮箱" />

      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button size="small" type="primary" @click="openEditDialog(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteUser(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增用户弹窗 -->
    <el-dialog title="新增用户" v-model="dialogVisible" width="500px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="姓名">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="学号">
          <el-input v-model="form.userStudentId" />
        </el-form-item>
        <el-form-item label="班级">
          <el-input v-model="form.userClass" />
        </el-form-item>
        <el-form-item label="部门">
          <el-input v-model="form.userDepartment" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.userEmail" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addUser">确定</el-button>
      </template>
    </el-dialog>

    <!-- 编辑用户弹窗 -->
    <el-dialog title="编辑用户" v-model="editDialogVisible" width="500px">
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="姓名">
          <el-input v-model="editForm.username" />
        </el-form-item>
        <el-form-item label="学号">
          <el-input v-model="editForm.userStudentId"  />
        </el-form-item>
        <el-form-item label="班级">
          <el-input v-model="editForm.userClass" />
        </el-form-item>
        <el-form-item label="部门">
          <el-input v-model="editForm.userDepartment" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="editForm.userEmail" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveEdit">保存</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<style scoped>
.card {
  padding: 16px;
}
</style>
